<template>
  <div>
    <el-dialog :title="title" :visible="visible">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="form.description" />
        </el-form-item>
        <el-form-item label="开启">
          <el-switch
            v-model="form.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="submitFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addpermissionAPI, getpermissionAPI, setpermissionAPI } from '@/api/permission'
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    list: {
      type: Array,
      default: null
    }
  },
  data() {
    return {
      form: {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: null,
        enVisible: null
      }, // 对话框表单
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancelFn() { // 取消方法
      this.form = {}
      this.$emit('update:visible', false)
    },
    async submitFn() {
      if (this.name === '添加权限') {
        await addpermissionAPI(this.form)
        this.$message.success('权限添加成功')
      } else {
        await setpermissionAPI(this.form)
        this.$message.success('权限修改成功')
      }
      this.$bus.$emit('londing')
      this.cancelFn()
    },
    async getpermissionFn(id) {
      this.form = await getpermissionAPI(id)
    }
  }
}
</script>

<style>
</style>
